
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

  <title>LINK_LIBRARIES_ONLY_TARGETS &mdash; CMake 3.27.4 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../_static/sphinx_highlight.js"></script>
    
    <link rel="shortcut icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="LINK_LIBRARY_OVERRIDE" href="LINK_LIBRARY_OVERRIDE.html" />
    <link rel="prev" title="LINK_LIBRARIES" href="LINK_LIBRARIES.html" />
 

  </head><body>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="LINK_LIBRARY_OVERRIDE.html" title="LINK_LIBRARY_OVERRIDE"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="LINK_LIBRARIES.html" title="LINK_LIBRARIES"
             accesskey="P">previous</a> |</li>
  <li>
    <img src="../_static/cmake-logo-16.png" alt=""
         style="vertical-align: middle; margin-top: -2px" />
  </li>
  <li>
    <a href="https://cmake.org/">CMake</a> &#187;
  </li>
  <li>
    <a href="../index.html">3.27.4 Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-properties.7.html" accesskey="U">cmake-properties(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">LINK_LIBRARIES_ONLY_TARGETS</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="link-libraries-only-targets">
<span id="prop_tgt:LINK_LIBRARIES_ONLY_TARGETS"></span><h1>LINK_LIBRARIES_ONLY_TARGETS<a class="headerlink" href="#link-libraries-only-targets" title="Permalink to this heading">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.23.</span></p>
</div>
<p>Enforce that link items that can be target names are actually existing targets.</p>
<p>Set this property to a true value to enable additional checks on the contents
of the <span class="target" id="index-0-prop_tgt:LINK_LIBRARIES"></span><a class="reference internal" href="LINK_LIBRARIES.html#prop_tgt:LINK_LIBRARIES" title="LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">LINK_LIBRARIES</span></code></a> and <span class="target" id="index-0-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a>
target properties, typically populated by <span class="target" id="index-0-command:target_link_libraries"></span><a class="reference internal" href="../command/target_link_libraries.html#command:target_link_libraries" title="target_link_libraries"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_libraries()</span></code></a>.
Checks are also applied to libraries added to a target through the
<span class="target" id="index-0-prop_tgt:INTERFACE_LINK_LIBRARIES_DIRECT"></span><a class="reference internal" href="INTERFACE_LINK_LIBRARIES_DIRECT.html#prop_tgt:INTERFACE_LINK_LIBRARIES_DIRECT" title="INTERFACE_LINK_LIBRARIES_DIRECT"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES_DIRECT</span></code></a> properties of its dependencies.
CMake will verify that link items that might be target names actually name
existing targets.  An item is considered a possible target name if:</p>
<ul class="simple">
<li><p>it does not contain a <code class="docutils literal notranslate"><span class="pre">/</span></code> or <code class="docutils literal notranslate"><span class="pre">\</span></code>, and</p></li>
<li><p>it does not start in <code class="docutils literal notranslate"><span class="pre">-</span></code>, and</p></li>
<li><p>(for historical reasons) it does not start in <code class="docutils literal notranslate"><span class="pre">$</span></code> or <code class="docutils literal notranslate"><span class="pre">`</span></code>.</p></li>
</ul>
<p>This property is initialized by the value of the
<span class="target" id="index-0-variable:CMAKE_LINK_LIBRARIES_ONLY_TARGETS"></span><a class="reference internal" href="../variable/CMAKE_LINK_LIBRARIES_ONLY_TARGETS.html#variable:CMAKE_LINK_LIBRARIES_ONLY_TARGETS" title="CMAKE_LINK_LIBRARIES_ONLY_TARGETS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_LINK_LIBRARIES_ONLY_TARGETS</span></code></a> variable when a non-imported
target is created.  The property may be explicitly enabled on an imported
target to check its link interface.</p>
<p>In the following example, CMake will halt with an error at configure time
because <code class="docutils literal notranslate"><span class="pre">miLib</span></code> is not a target:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">set(</span><span class="no">CMAKE_LINK_LIBRARIES_ONLY_TARGETS</span><span class="w"> </span><span class="no">ON</span><span class="nf">)</span>
<span class="nf">add_library(</span><span class="nb">myLib</span><span class="w"> </span><span class="no">STATIC</span><span class="w"> </span><span class="nb">myLib.c</span><span class="nf">)</span>
<span class="nf">add_executable(</span><span class="nb">myExe</span><span class="w"> </span><span class="nb">myExe.c</span><span class="nf">)</span>
<span class="nf">target_link_libraries(</span><span class="nb">myExe</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">miLib</span><span class="nf">)</span><span class="w"> </span><span class="c"># typo for myLib</span>
</pre></div>
</div>
<p>In order to link toolchain-provided libraries by name while still
enforcing <code class="docutils literal notranslate"><span class="pre">LINK_LIBRARIES_ONLY_TARGETS</span></code>, use an
<a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">imported</span></a>
<a class="reference internal" href="../manual/cmake-buildsystem.7.html#interface-libraries"><span class="std std-ref">Interface Library</span></a> with the
<span class="target" id="index-0-prop_tgt:IMPORTED_LIBNAME"></span><a class="reference internal" href="IMPORTED_LIBNAME.html#prop_tgt:IMPORTED_LIBNAME" title="IMPORTED_LIBNAME"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">IMPORTED_LIBNAME</span></code></a> target property:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_library(</span><span class="nb">toolchain</span><span class="o">::</span><span class="nb">m</span><span class="w"> </span><span class="no">INTERFACE</span><span class="w"> </span><span class="no">IMPORTED</span><span class="nf">)</span>
<span class="nf">set_property(</span><span class="no">TARGET</span><span class="w"> </span><span class="nb">toolchain</span><span class="o">::</span><span class="nb">m</span><span class="w"> </span><span class="no">PROPERTY</span><span class="w"> </span><span class="no">IMPORTED_LIBNAME</span><span class="w"> </span><span class="s">&quot;m&quot;</span><span class="nf">)</span>
<span class="nf">target_link_libraries(</span><span class="nb">myExe</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">toolchain</span><span class="o">::</span><span class="nb">m</span><span class="nf">)</span>
</pre></div>
</div>
<p>See also policy <span class="target" id="index-0-policy:CMP0028"></span><a class="reference internal" href="../policy/CMP0028.html#policy:CMP0028" title="CMP0028"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0028</span></code></a>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If <span class="target" id="index-1-prop_tgt:INTERFACE_LINK_LIBRARIES"></span><a class="reference internal" href="INTERFACE_LINK_LIBRARIES.html#prop_tgt:INTERFACE_LINK_LIBRARIES" title="INTERFACE_LINK_LIBRARIES"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">INTERFACE_LINK_LIBRARIES</span></code></a> contains generator expressions,
its actual list of link items may depend on the type and properties of
the consuming target.  In such cases CMake may not always detect names
of missing targets that only appear for specific consumers.
A future version of CMake with improved heuristics may start triggering
errors on projects accepted by previous versions of CMake.</p>
</div>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="LINK_LIBRARIES.html"
                          title="previous chapter">LINK_LIBRARIES</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="LINK_LIBRARY_OVERRIDE.html"
                          title="next chapter">LINK_LIBRARY_OVERRIDE</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/prop_tgt/LINK_LIBRARIES_ONLY_TARGETS.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="LINK_LIBRARY_OVERRIDE.html" title="LINK_LIBRARY_OVERRIDE"
             >next</a> |</li>
        <li class="right" >
          <a href="LINK_LIBRARIES.html" title="LINK_LIBRARIES"
             >previous</a> |</li>
  <li>
    <img src="../_static/cmake-logo-16.png" alt=""
         style="vertical-align: middle; margin-top: -2px" />
  </li>
  <li>
    <a href="https://cmake.org/">CMake</a> &#187;
  </li>
  <li>
    <a href="../index.html">3.27.4 Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-properties.7.html" >cmake-properties(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">LINK_LIBRARIES_ONLY_TARGETS</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2000-2023 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.3.0.
    </div>
  </body>
</html>